#!/bin/bash
# Copyright 2018 Johan Gunnarsson <johan.gunnarsson@gmail.com>
#
# This file is part of vlc-bittorrent.
#
# vlc-bittorrent is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# vlc-bittorrent is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with vlc-bittorrent.  If not, see <http://www.gnu.org/licenses/>.

set -o pipefail

mkdir -p $HOME/.config/vlc
mkdir -p $HOME/.cache/vlc
mkdir -p $HOME/.local/share/vlc

run_test() {
	rm -rf /tmp/vlc-bittorrent
	mkdir -p /tmp/vlc-bittorrent
	pushd data
	../miniclient *.torrent >/dev/null &
	popd
	sleep 1
	echo ./downloaddummy "$3" $2
	(timeout 60 ./downloaddummy "$3" $2 || :) 2>/dev/null |
	grep DOWNLOADDUMMY |
	diff -U 10 "$4" - |
	sed -e "s/^/# /" &&
	echo "ok # $1 $3" ||
	echo "not ok # $1 $3"
	kill -9 %1
}

run_metadata_test() {
	run_test "metadata" "--show-metadata" $*
}

run_read_test() {
	run_test "read" "--show-read" $*
}

echo "1..16"
run_metadata_test "file://./data/flags.torrent" "data/flags.torrent.downloaddummy-metadata-expected.txt"
run_metadata_test "file://./data/nasa1.ogv.torrent" "data/nasa1.ogv.torrent.downloaddummy-metadata-expected.txt"
run_metadata_test "file://./data/nasa.torrent" "data/nasa.torrent.downloaddummy-metadata-expected.txt"
run_metadata_test "file://./data/sweden.png.torrent" "data/sweden.png.torrent.downloaddummy-metadata-expected.txt"
run_metadata_test "magnet:?xt=urn:btih:18945a9300abfe4ff2442559bb08b8ddb357c16f&dn=flags" "data/flags.torrent-magnet.downloaddummy-metadata-expected.txt"
run_metadata_test "magnet:?xt=urn:btih:8a32f3f6f3c9125da79e29c869122758004ee837&dn=nasa1.ogv" "data/nasa1.ogv.torrent-magnet.downloaddummy-metadata-expected.txt"
run_metadata_test "magnet:?xt=urn:btih:6fa46c9a0bb4eecb837c25845d39c5324be66401&dn=nasa" "data/nasa.torrent-magnet.downloaddummy-metadata-expected.txt"
run_metadata_test "magnet:?xt=urn:btih:fce002e43ed1159f4612982ce8fcdb9d30e48f1e&dn=sweden.png" "data/sweden.png.torrent-magnet.downloaddummy-metadata-expected.txt"
run_read_test "file://./data/flags.torrent" "data/flags.torrent.downloaddummy-read-expected.txt"
run_read_test "file://./data/nasa1.ogv.torrent" "data/nasa1.ogv.torrent.downloaddummy-read-expected.txt"
run_read_test "file://./data/nasa.torrent" "data/nasa.torrent.downloaddummy-read-expected.txt"
run_read_test "file://./data/sweden.png.torrent" "data/sweden.png.torrent.downloaddummy-read-expected.txt"
run_read_test "magnet:?xt=urn:btih:18945a9300abfe4ff2442559bb08b8ddb357c16f&dn=flags" "data/flags.torrent-magnet.downloaddummy-read-expected.txt"
run_read_test "magnet:?xt=urn:btih:8a32f3f6f3c9125da79e29c869122758004ee837&dn=nasa1.ogv" "data/nasa1.ogv.torrent-magnet.downloaddummy-read-expected.txt"
run_read_test "magnet:?xt=urn:btih:6fa46c9a0bb4eecb837c25845d39c5324be66401&dn=nasa" "data/nasa.torrent-magnet.downloaddummy-read-expected.txt"
run_read_test "magnet:?xt=urn:btih:fce002e43ed1159f4612982ce8fcdb9d30e48f1e&dn=sweden.png" "data/sweden.png.torrent-magnet.downloaddummy-read-expected.txt"
exit 0
